home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 275_02 / prob22.c < prev    next >
Text File  |  1980-01-01  |  28KB  |  971 lines

  1.  
  2. /* prob22.c                        */
  3. /* program for LCA22 option 't'                */
  4. /* calculate probabilities related to evolution        */
  5. /* Harold V. McIntosh, 10 August 1987            */
  6. /* 29 February 1988 - adapted from LCA21 to LCA22    */
  7.  
  8. /* references:                            */
  9. /*                                */
  10. /* W. John Wilbur, David J. Lipman and Shihab A. Shamma        */
  11. /* On the prediction of local patterns in cellular automata    */
  12. /* Physica 19D 397-410 (1986)                    */ 
  13. /*                                */
  14. /* Howard A. Gutowitz, Jonathan D. Victor and Bruce W. Knight    */
  15. /* Local structure theory for cellular automata            */
  16. /* Physica 28D 18-48 (1987)                    */
  17.  
  18. /*  Copyright (C) 1987  */
  19. /*  Copyright (C) 1988  */
  20. /*  Harold V. McIntosh  */
  21. /*  Gerardo Cisneros S. */
  22.  
  23. # define BROW        3    /* row for bar charts        */
  24. # define EROW        1    /* row for evolution synopsis    */
  25. # define AORG         0    /* x-origin  of contour plot    */
  26. # define BORG       109    /* x-origin  of 2-block param   */
  27. # define CORG       219    /* x-origin  of Bernstein plot  */
  28.  
  29. /* edit the probability screen    */
  30. edtri() {char c;
  31.  
  32.   videomode(COLGRAF);
  33.   videopalette(YELREGR);
  34.  
  35.   while (0<1) {
  36.   videocursor(0,0,0);
  37.   videoputc(':',2);
  38.   scrrul();
  39.   videocursor(0,0,36);
  40.   videoputc('?',2);
  41.   c=kbdin();
  42.   if (c == '\015') break;
  43.   videocursor(0,0,38);
  44.   videoputc(c,2);
  45.   videocursor(0,0,36);
  46.   videoputc(' ',0);
  47.   switch (c) {
  48.     case '+': videopalette(WHCYMAG); break;
  49.     case '-': videopalette(YELREGR); break;
  50.     case 'a': asfreq(3); break;
  51.     case 'e': pevolve(); break;
  52.     case 'g': lifreq(50,2); break;
  53.     case 'G': lifreq(200,1); break;
  54.     case 'm': moncar(1,2); break;
  55.     case 'M': moncar(50,1); break;
  56.     case 'x': pdiff(100); break;
  57.     case 'i': pidiff(100); break;
  58.     case 'j': pjdiff(100); break;
  59.     case 'y': pdiff3(100); break;
  60.     case 'z': pdiff4(100); break;
  61.     case 't': twoblockfreq(100); break;
  62.     case '1': nblclr(); oneblfreq(8*BROW,300,48); break;
  63.     case '2': nblclr(); twoblfreq(8*BROW,300,48); break;
  64.     case '3': nblclr(); thrblfreq(8*BROW,300,48); break;
  65.     case '4': nblclr(); foublfreq(8*BROW,300,48); break;
  66.     case '5': nblclr(); fivblfreq(8*BROW,300,48); break;
  67.     case '6': nblclr(); sixblfreq(8*BROW,300,48); break;
  68.     case '?': videomode(COLGRAF); videopalette(YELREGR); trmenu(); break;
  69.     case '/': videomode(COLGRAF); videopalette(YELREGR); break;
  70.     default: break;
  71.     }; /* end switch */
  72.   };   /* end while  */
  73.   videopalette(WHCYMAG);
  74.   videomode(T80X25);
  75. }      /* end edtri  */
  76.  
  77. /* show menu */
  78. trmenu() {
  79.   videoscroll(BROW,0,BROW+8,40,0,0);
  80.   videocursor(0,BROW,0);
  81.   printf("a       - a priori estimates\n");
  82.   printf("m,M,g,G - sample evolution\n");
  83.   printf("xyz     - selfconsistent probabilities\n");
  84.   printf("xij     - iterated s-c probabilities\n");
  85.   printf("t       - graph 2 block probabilities\n");
  86.   printf("123456  - n-block bar charts\n");
  87.   printf("+-      - change color pallette\n");
  88.   printf("e       - 12 lines evolution\n");
  89.   printf("/?(clear screen, show menu), <cr>(exit)\n");
  90. }
  91.  
  92. /* show twelve lines of evolution at top of screen */
  93. pevolve() {int i, j;
  94.   videoscroll(EROW,0,EROW+1,40,0,0);
  95.   asctobin();
  96.   for (j=8*EROW; j<8*(EROW+2)-3; j++) {
  97.     for (i=0; i<AL; i++) videodot(j,i,arr1[i]);
  98.     onegen(AL);
  99.     };
  100. }
  101.  
  102. /* Clear a space for the n-block frequencies */
  103. nblclr() {videoscroll(BROW,0,BROW+8,40,0,0);}
  104.  
  105. /* make a frame for a graph          */
  106. /* (x,y) = lower left corner; e.g. (0,0) */
  107. /* n     = dimension of frame            */
  108. gfram(x,y,n) int x, y, n; {int i;
  109.  
  110. for (i=0; i<=n; i++) videodot(199-y-i,x,0);
  111. for (i=0; i<=n; i++) videodot(199-y-i,x+n,0);
  112. for (i=0; i<=n; i++) videodot(199-n-y,x+i,0);
  113. for (i=0; i<=n; i++) videodot(199-y,x+i,0);
  114.  
  115. for (i=0; i<=n; i+=10) videodot(199-y-i,x,3);
  116. for (i=0; i<=n; i+=10) videodot(199-y-i,x+n,3);
  117. for (i=0; i<=n; i+=10) videodot(199-n-y,x+i,3);
  118. for (i=0; i<=n; i+=10) videodot(199-y,x+i,3);
  119. }
  120.  
  121. /* put a diagonal in a graph */
  122. gdiag(x,y,n) int x, y, n; {int i;
  123. for (i=0; i<=n; i+=2) videodot(199-y-i,x+i,3);
  124. }
  125.  
  126. /* graph Bernstein polynomial */
  127. bgraf(x,y,k,n) int x, y, k, n; {int i; double bern(), en, dp, p;
  128. if (n==0) return;
  129. en=(double)(n); dp=1.0/en;
  130. for (i=0,p=0.0; i<n; i++,p+=dp) {videodot(199-y-(int)(en*bern(p,k)),x+i,1);};
  131. }
  132.  
  133. /* "Monte Carlo" determination of probabilities */
  134. moncar(n,l) int n, l; {
  135. int i, j, k, b[KK], bb[KK][KK];
  136. double bf[KK], bbf[KK][KK];
  137.  
  138. nblclr();
  139. gfram(BORG,0,100);
  140. asctobin();
  141. for (k=0; k<n; k++) {
  142.   onegen(AL);
  143.   for (i=0; i<KK; i++) b[i]=0;
  144.   for (i=0; i<AL; i++) b[arr1[i]]++;
  145.   for (i=0; i<KK; i++) bf[i]=((double)(b[i]))/((double)(AL));
  146.   for (i=0; i<KK; i++) for (j=0; j<KK; j++) bb[i][j]=0;
  147.   for (i=1; i<AL; i++) bb[arr1[i-1]][arr1[i]]++;
  148.   bb[arr1[AL-1]][arr1[0]]++; 
  149.   for (i=0; i<KK; i++) for (j=0; j<KK; j++) 
  150.     bbf[i][j]=((double)(bb[i][j]))/((double)(AL));
  151.   videodot(199-(int)(100.0*bbf[1][1]),BORG+(int)(100.0*bf[1]),l);
  152.   };
  153.  videocursor(0,BROW+7,0);
  154. printf("(Monte Carlo) "); 
  155. for (i=0; i<KK; i++) printf("%2d:%5.3f ",i,bf[i]);
  156. videocursor(0,BROW+8,0);
  157. for (i=0; i<KK; i++) for (j=0; j<KK; j++) 
  158.   printf("%1d%1d:%5.3f ",i,j,bbf[i][j]);
  159. }
  160.  
  161. /* Generate coefficients of 2nd generation Bernstein Polynomial */
  162. berncoef() {
  163. int i, i0, i1, i2, i3, i4;
  164.  
  165. for (i=0; i<BD; i++) bp[i]=0.0;
  166. for (i0=0; i0<KK; i0++) {
  167. for (i1=0; i1<KK; i1++) {
  168. for (i2=0; i2<KK; i2++) {
  169. for (i3=0; i3<KK; i3++) {
  170. for (i4=0; i4<KK; i4++) {
  171.   if (ascrule[i0][i1][i2][i3][i4]=='1') bp[i0+i1+i2+i3+i4]+=1.0;
  172.   };};};};};
  173. }
  174.  
  175. /* Generate coefficients of 3rd generation Bernstein Polynomial */
  176. bernthrd() {
  177. int i, i0, i1, i2, i3, i4, i5, i6, i7, i8;
  178. int j0, j1, j2, j3, j4;
  179.  
  180. asctobin();
  181. for (i=0; i<BD; i++) bp[i]=0.0;
  182. for (i0=0; i0<KK; i0++) {
  183. for (i1=0; i1<KK; i1++) {
  184. for (i2=0; i2<KK; i2++) {
  185. for (i3=0; i3<KK; i3++) {
  186. for (i4=0; i4<KK; i4++) {
  187. for (i5=0; i5<KK; i5++) {
  188. for (i6=0; i6<KK; i6++) {
  189. for (i7=0; i7<KK; i7++) {
  190. for (i8=0; i8<KK; i8++) {
  191.   j0=binrule[i0][i1][i2][i3][i4];
  192.   j1=binrule[i1][i2][i3][i4][i5];
  193.   j2=binrule[i2][i3][i4][i5][i6];
  194.   j3=binrule[i3][i4][i5][i6][i7];
  195.   j4=binrule[i4][i5][i6][i7][i8];
  196.   if (ascrule[j0][j1][j2][j3][j4]=='1') bp[i0+i1+i2+i3+i4+i5+i6+i7+i8]+=1.0;
  197.   };};};};};};};};};
  198. }
  199.  
  200. /* Generate coefficients of 4th generation Bernstein Polynomial */
  201. bernfrth() {
  202. int i, i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, ia, ib, ic;
  203. int j0, j1, j2, j3, j4, j5, j6, j7, j8;
  204. int k0, k1, k2, k3, k4;
  205.  
  206. asctobin();
  207. for (i=0; i<BD; i++) bp[i]=0.0;
  208. for (i0=0; i0<KK; i0++) {
  209. for (i1=0; i1<KK; i1++) {
  210. for (i2=0; i2<KK; i2++) {
  211. for (i3=0; i3<KK; i3++) {
  212. for (i4=0; i4<KK; i4++) {
  213. for (i5=0; i5<KK; i5++) {
  214. for (i6=0; i6<KK; i6++) {
  215. for (i7=0; i7<KK; i7++) {
  216. for (i8=0; i8<KK; i8++) {
  217. for (i9=0; i9<KK; i9++) {
  218. for (ia=0; ia<KK; ia++) {
  219. for (ib=0; ib<KK; ib++) {
  220. for (ic=0; ic<KK; ic++) {
  221.   j0=binrule[i0][i1][i2][i3][i4];
  222.   j1=binrule[i1][i2][i3][i4][i5];
  223.   j2=binrule[i2][i3][i4][i5][i6];
  224.   j3=binrule[i3][i4][i5][i6][i7];
  225.   j4=binrule[i4][i5][i6][i7][i8];
  226.   j5=binrule[i5][i6][i7][i8][i9];
  227.   j6=binrule[i6][i7][i8][i9][ia];
  228.   j7=binrule[i7][i8][i9][ia][ib];
  229.   j8=binrule[i8][i9][ia][ib][ic];
  230.   k0=binrule[j0][j1][j2][j3][j4];
  231.   k1=binrule[j1][j2][j3][j4][j5];
  232.   k2=binrule[j2][j3][j4][j5][j6];
  233.   k3=binrule[j3][j4][j5][j6][j7];
  234.   k4=binrule[j4][j5][j6][j7][j8];
  235.   i=i0+i1+i2+i3+i4+i5+i6+i7+i8+i9+ia+ib+ic;
  236.   if (ascrule[k0][k1][k2][k3][k4]=='1') bp[i]+=1.0;
  237.   };};};};};};};};};};};};};
  238. }
  239.  
  240. /* evaluate the nth generation Bernstein polynomial at point p */
  241. double bern(p,n) double p; int n; {double q, s, x, r; int i, d;
  242. d=4*n-3;
  243. if (p>0.99) return bp[d];
  244. q=1.0-p; r=p/q; s=0.0; x=1.0;
  245. for (i=0; i<d;  i++) x*=q;
  246. for (i=0; i<=d; i++, x*=r) s+=bp[i]*x;
  247. s*=0.9998;
  248. return(s+0.0001);
  249. }
  250.  
  251. /* graph the probability of the second generation */
  252. pdiff(n) int n; {
  253. berncoef();
  254. gfram(CORG,0,n);
  255. gdiag(CORG,0,n);
  256. bgraf(CORG,0,2,n);
  257. }
  258.  
  259. /* graph the iterated probability of the second generation */
  260. pidiff(n) int n; {int i; double bern(), en, p;
  261. en=1.0/((double)(n));
  262. berncoef();
  263. gdiag(CORG,0,n);
  264. for (i=0; i<=n; i++) {
  265.   p=((double)(i))*en;
  266.   videodot(199-(int)(100.0*bern(bern(p,